package cn.nange.cmfz.controller;

import cn.nange.cmfz.entity.Article;
import cn.nange.cmfz.service.ArticleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("add")
    @ResponseBody
    public Map add(Article b) {
        HashMap map = new HashMap();
        try {
            articleService.add(b);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("showOne")
    public Article showOne(Integer id) {
        System.out.println(id);
        Article article = articleService.showOne(id);
        System.out.println("article = " + article);
        return article;
    }

    @ResponseBody
    @RequestMapping("update")
    public Map update(Article b) {
        HashMap map = new HashMap();
        try {
            articleService.update(b);
            map.put("isUpdate", true);

        } catch (Exception e) {

            e.printStackTrace();

            map.put("isUpdate", false);
        }
        System.out.println("update:"+b);
        return map;
    }

    @ResponseBody
    @RequestMapping("delete")
    public Map delete(int[] id) {
        HashMap map = new HashMap();
        try {
            for (int i = 0; i < id.length; i++) {
                articleService.delete(id[i]);
                System.out.println(id[i]);
                map.put("isDelete", true);
            }

        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete", false);
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("show")
    public Map show(Integer page, Integer limit) {

        PageInfo<Article> show = articleService.showAll(page, limit);
        HashMap map = new HashMap();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", show.getTotal());
        map.put("data", show.getList());
        return map;
    }

    @ResponseBody
    @RequestMapping("upload")
    public Map upload(MultipartFile file) throws IOException {
        String s = UUID.randomUUID().toString() + "-" + file.getOriginalFilename();
//        file.transferTo(new File("D://static",s));
        file.transferTo(new File("/Users/nange/static/cmfz", s));
        HashMap map = new HashMap();
        map.put("code", 0);
        map.put("img", s);
        System.out.println("upload:" + map);
        return map;
    }
}
